﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using Tesla.Core;
using Tesla.Math;

namespace Tesla.Scene.Shape {
    /// <summary>
    /// Pyramid shape defined by a square base with four faces that converge to a central point at the top. The center of the period is
    /// always the origin (0, 0, 0) which is located directly inside of the pyramid's center of gravity (not center of its base). The pyramid is axis-aligned,
    /// with its point +Y and its base on the X-Z plane
    /// </summary>
    public class Pyramid : Mesh {
        private float _baseWidth;
        private float _height;


        /// <summary>
        /// Creates a new instance of <see cref="Pyramid"/>. Base width and height are both one.
        /// </summary>
        /// <param name="name">The name of the mesh.</param>
        public Pyramid(String name)
            : base(name) {
                _baseWidth = 1;
                _height = 1;
                MeshData.UseDynamicIndexBuffer = false;
                MeshData.UseDynamicVertexBuffer = false;
                MeshData.UseIndexedPrimitives = true;
                SetVertexData();
        }

        /// <summary>
        /// Creates a new instance of <see cref="Pyramid"/>.
        /// </summary>
        /// <param name="name">The name of the mesh.</param>
        /// <param name="baseWidth">Width of the base.</param>
        /// <param name="height">The height of the pyramid.</param>
        public Pyramid(String name, float baseWidth, float height)
            : base(name) {
                _baseWidth = baseWidth;
                _height = height;
                MeshData.UseDynamicIndexBuffer = false;
                MeshData.UseDynamicVertexBuffer = false;
                MeshData.UseIndexedPrimitives = true;
                SetVertexData();
        }

        private void SetVertexData() {
            Vector3[] verts = new Vector3[5];
            
            //Base quad
            verts[0] = new Vector3(-_baseWidth / 2, -_height / 2, -_baseWidth / 2);
            verts[1] = new Vector3(_baseWidth / 2, -_height / 2, -_baseWidth / 2);
            verts[2] = new Vector3(_baseWidth / 2, -_height / 2, _baseWidth / 2);
            verts[3] = new Vector3(-_baseWidth / 2, -_height / 2, _baseWidth / 2);

            //Point on top
            verts[4] = new Vector3(0, _height / 2, 0);

            DataBuffer<Vector3> positions = new DataBuffer<Vector3>(16);
            DataBuffer<Vector3> normals = new DataBuffer<Vector3>(16);
            DataBuffer<Vector2> texCoords = new DataBuffer<Vector2>(16);

            //Setup base
            positions.Set(verts[0]);
            normals.Set(Vector3.Down);
            texCoords.Set(Vector2.Zero);

            positions.Set(verts[1]);
            normals.Set(Vector3.Down);
            texCoords.Set(new Vector2(1, 0));

            positions.Set(verts[2]);
            normals.Set(Vector3.Down);
            texCoords.Set(new Vector2(1, 1));

            positions.Set(verts[3]);
            normals.Set(Vector3.Down);
            texCoords.Set(new Vector2(0, 1));

            float cos = (float) System.Math.Cos(MathHelper.ToRadians(45.0f));

            //Front face
            positions.Set(verts[2]);
            normals.Set(new Vector3(0, cos, cos));
            texCoords.Set(new Vector2(0, 1));

            positions.Set(verts[4]);
            normals.Set(new Vector3(0, cos, cos));
            texCoords.Set(new Vector2(.5f, 0));

            positions.Set(verts[3]);
            normals.Set(new Vector3(0, cos, cos));
            texCoords.Set(new Vector2(1, 1));

            //Back face
            positions.Set(verts[0]);
            normals.Set(new Vector3(0, cos, -cos));
            texCoords.Set(new Vector2(0, 1));

            positions.Set(verts[4]);
            normals.Set(new Vector3(0, cos, -cos));
            texCoords.Set(new Vector2(.5f, 0));

            positions.Set(verts[1]);
            normals.Set(new Vector3(0, cos, -cos));
            texCoords.Set(new Vector2(1, 1));

            //Left face
            positions.Set(verts[1]);
            normals.Set(new Vector3(cos, cos, 0));
            texCoords.Set(new Vector2(0, 1));

            positions.Set(verts[4]);
            normals.Set(new Vector3(cos, cos, 0));
            texCoords.Set(new Vector2(.5f, 0));

            positions.Set(verts[2]);
            normals.Set(new Vector3(cos, cos,0));
            texCoords.Set(new Vector2(1, 1));

            //Right face
            positions.Set(verts[3]);
            normals.Set(new Vector3(-cos, cos, 0));
            texCoords.Set(new Vector2(0, 1));

            positions.Set(verts[4]);
            normals.Set(new Vector3(-cos, cos, 0));
            texCoords.Set(new Vector2(.5f, 0));

            positions.Set(verts[0]);
            normals.Set(new Vector3(-cos, cos, 0));
            texCoords.Set(new Vector2(1, 1));


            //Setup indices
            short[] temp = new short[] { 2, 1, 0, 3, 2, 0, 6, 5, 4, 9, 8, 7, 12, 11, 10, 15, 14, 13 };
            MeshData.Indices = new DataBuffer<short>(temp);
            MeshData.Positions = positions;
            MeshData.Normals = normals;
            MeshData.TextureCoordinates = texCoords;
            MeshData.Reconstruct();
        }
    }
}
